package com.myapplication.interfaces;

import com.myapplication.callback.ChangePassowrdCallback;
import com.myapplication.http.util.Util;

import org.xutils.common.Callback;
import org.xutils.ex.HttpException;
import org.xutils.http.HttpMethod;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Administrator on 2016/6/8.
 */
public class ChangePasswordConnect implements ChangePasswordInter {
    @Override
    public void changePassword(String mobile, String code, String password, String client,
                               final ChangePassowrdCallback changePassowrdCallback) {
        RequestParams requestParams = new RequestParams(Util.UrlChangePassword);
        requestParams.addBodyParameter("mobile",mobile);
        requestParams.addBodyParameter("code",code);
        requestParams.addBodyParameter("password",password);
        requestParams.addBodyParameter("client",client);
        requestParams.setConnectTimeout(5000);
        x.http().request(HttpMethod.POST, requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                changePassowrdCallback.OnChangePasswordSuccess(result);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(ex instanceof HttpException){
                    int code = ((HttpException) ex).getCode();
                    String errorResult = ((HttpException) ex).getResult();
                    changePassowrdCallback.OnChangePasswordSuccess(code+errorResult);
                }
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
